package org.example.ch04;

import org.example.ch04.tree.TreeNode;

/**
 * 翻转二叉树
 * https://leetcode-cn.com/problems/invert-binary-tree/
 */
public class InvertTree {
    public static void main(String[] args) {

        TreeNode node1 = new TreeNode(1);

        TreeNode node2 = new TreeNode(3,null,null);

        TreeNode node3 = new TreeNode(6,null,null);
        TreeNode node4 = new TreeNode(9,null,null);

        TreeNode node5 = new TreeNode(2,node1,node2);
        TreeNode node6 = new TreeNode(7,node3,node4);

        TreeNode root = new TreeNode(4,node5,node6);

        root = solution1(root);
        System.out.println(root);
    }

    public static TreeNode solution1(TreeNode root){
        //递归的终结条件
        if(root == null){
            return null;
        }
        //处理当前层逻辑
        TreeNode tmp = root.right;
        root.right = root.left;
        root.left = tmp;

        //下探下一次递归
        //递归交换当前树的左节点
        solution1(root.left);
        //递归交换当前树的右节点
        solution1(root.right);

        //函数返回的时候就表示当前这个节点，以及它的所有左右子树都交换换了
        return root;
        //清理当前层

    }
}
